/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package transaction.committee_management;

import common.util.HibernateUtil;
import java.awt.HeadlessException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import system.SystemMassages;
import template.DefaultRegistrationForm;
import template.RegistrantionPanel;
import template.SERDefaultRegistrationForm;
import transaction.committee_management.object.YearCommittee;

/**
 *
 * @author User
 */
public class REGCommitteeManager extends DefaultRegistrationForm {

    @Override
    public RegistrantionPanel objectCreator() {
        addCommittee = new PCCommitteeManager();
        return addCommittee;
    }

    @Override
    public Object initObject(RegistrantionPanel panel) {
        return panel.initObject();
    }

    @Override
    protected String setFrameTitle() {
        return TITLE;
    }

    @Override
    protected void getAutoFillComponents(RegistrantionPanel panel) {
    }

    @Override
    protected String getViewDataList() {
        return "from transaction.add_committee.object.Committee";
    }

    @Override
    protected void save() {

        Object[] initObject = addCommittee.initObjectList();
        System.gc();
        Session session = HibernateUtil.getSession();
        Transaction transaction = session.beginTransaction();
        try {
            for (Object object : initObject) {
                String hql = "update transaction.committee_management.object.YearCommittee as yc set yc.status=:status where yc.year=:year and yc.member=:member and yc.committee=:committee";
                Query query = session.createQuery(hql);
                query.setParameter("status", ((YearCommittee)object).getStatus());
                query.setParameter("year", ((YearCommittee)object).getYear());
                query.setParameter("committee", ((YearCommittee)object).getCommittee());
                query.setParameter("member", ((YearCommittee)object).getMember());
                query.executeUpdate();
            }
            transaction.commit();
            addCommittee.resetAll();
            JOptionPane.showMessageDialog(null, SystemMassages.SAVED_SUCCESS);
        } catch (HibernateException | HeadlessException e) {
            transaction.rollback();
            JOptionPane.showMessageDialog(null, SystemMassages.SAVING_FAIL);
            Logger.getLogger(SERDefaultRegistrationForm.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            HibernateUtil.closeSession();
//            SystemVariyable.session.close();
        }
    }

    private static final String TITLE = "Add Committee";
    private PCCommitteeManager addCommittee;
}
